package com.aaron.kjviewutils.driving;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.BaseActivity;
import com.aaron.kjviewutils.NetworkImageHolderView;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecyclerDividerForOther;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Aaron on 2017/3/3.
 */

public class DrivingActivity extends BaseActivity {

    @Bind(R.id.drivingToolBar)
    Toolbar drivingToolBar;
    @Bind(R.id.barTitle)
    TextView barTitle;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;
    @Bind(R.id.drivingBanner)
    ConvenientBanner drivingBanner;
    @Bind(R.id.drivingRecyclerView)
    RecyclerView drivingRecyclerView;

    private List<String> bannerUrls;
    private List<DrivingMenuBean> drivingMenus;
    private DrivingMenuAdapter adapter;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        //关闭
        closeSelf(DrivingActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_driving);
        ButterKnife.bind(this);
        initTitle();
        initBanner();
        initRecycler();
    }

    private void initRecycler()
    {
        drivingMenus = new ArrayList<DrivingMenuBean>();
        DrivingMenuBean menu01 = new DrivingMenuBean();
        menu01.id = 1l;
        menu01.menuTag = "no.1";
        menu01.menuName = "科目一题库";
        menu01.menuIcon = "http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/css/images/course/suiji.png";
        drivingMenus.add(menu01);
        DrivingMenuBean menu02 = new DrivingMenuBean();
        menu02.id = 2l;
        menu02.menuTag = "no.4";
        menu02.menuName = "科目四题库";
        menu02.menuIcon = "http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/css/images/course/chapter.png";
        drivingMenus.add(menu02);
        DrivingMenuBean menu03 = new DrivingMenuBean();
        menu03.id = 3l;
        menu03.menuTag = "no.3";
        menu03.menuName = "专项题库分类";
        menu03.menuIcon = "http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/css/images/course/nanti.png";
        drivingMenus.add(menu03);
        // 错列网格布局
        drivingRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL));
        adapter = new DrivingMenuAdapter(DrivingActivity.this, drivingMenus);
        drivingRecyclerView.setAdapter(adapter);
        drivingRecyclerView.addItemDecoration(new RecyclerDividerForOther(DrivingActivity.this, 2));
        drivingRecyclerView.setItemAnimator(new DefaultItemAnimator()); // 默认动画
        adapter.setmOnItemClickListener(new DrivingMenuAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //点击事件
                DrivingMenuBean menu = drivingMenus.get(position);
                Bundle bundle = null;
                switch (menu.menuTag)
                {
                    case "no.1":
                    {
                        //科目一题库
                        bundle = new Bundle();
                        bundle.putString("type", "no.1");
                        showActivity(DrivingActivity.this, DrivingDocActivity.class, bundle);
                    }
                    break;
                    case "no.3":
                    {
                        //专项题库分类
                        showActivity(DrivingActivity.this, DrivingTopicActivity.class);
                    }
                    break;
                    case "no.4":
                    {
                        //科目四题库
                        bundle = new Bundle();
                        bundle.putString("type", "no.4");
                        showActivity(DrivingActivity.this, DrivingDocActivity.class, bundle);
                    }
                    break;
                    default:
                        break;
                }
            }
        });
    }

    private void initBanner()
    {
        bannerUrls = new ArrayList<String>();
        bannerUrls.add("http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/images/banner/banner_download.png");
        bannerUrls.add("http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/images/banner/banner_ruzhu.png");
        bannerUrls.add("http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/images/banner/banner_new2.jpg");
        bannerUrls.add("http://web.resource.mucang.cn/jiakaobaodian.web/jkbd/resources/images/banner/banner_new1.png");
        drivingBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, bannerUrls).setPointViewVisible(true)//设置指示器是否可见
                .setPageIndicator(new int[]{R.drawable.banner_unchoice, R.drawable.banner_choice})//设置指示器圆点
                .startTurning(5000)     //设置自动切换（同时设置了切换时间间隔）
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT) //设置指示器位置（左、中、右）
                .setManualPageable(true); //设置点击监听事件;
        drivingBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                showMsg("你已点击了banner图");
            }
        });
    }



    private void initTitle()
    {
        drivingToolBar.setTitle("");
        barTitle.setText("驾考题库");
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        drivingToolBar.setNavigationIcon(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(null!=drivingBanner)
        {
            drivingBanner.startTurning(5000);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(null!=drivingBanner)
        {
            drivingBanner.stopTurning();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(DrivingActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
